package com.ry.core.base.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.ry.core.base.dao.hibernate.HibernateDao;
import com.ry.core.base.dao.mybatis.BatisDao;
import com.ry.core.base.model.SimpleModel;
import com.ry.core.exception.DaoException;

/**
 * 框架默认的dao层实现类，同时整合hibernate和mybatis
 * 
 * @author lqw
 * 
 */
@Repository
public class BaseDao {

	@Autowired
	public HibernateDao hibernateDao;

	@Autowired
	public BatisDao batisDao;

	/**
	 * 新增，由hibernate实现
	 * 
	 * @param object
	 * @throws Exception
	 */
	public void addDefault(SimpleModel object) throws DaoException {
		hibernateDao.add(object);
	}

	/**
	 * 修改，由hibernate实现
	 * 
	 * @param object
	 * @throws Exception
	 */
	public SimpleModel editDefault(SimpleModel object) throws DaoException {
		return hibernateDao.edit(object);
	}

	/**
	 * 删除，由hibernate实现
	 * 
	 * @param object
	 * @throws Exception
	 */
	public void removeDefault(SimpleModel object) throws DaoException {
		hibernateDao.remove(object);
	}

	/**
	 * 查询，由hibernate实现
	 * 
	 * @param object
	 * @param id
	 * @return
	 * @throws DaoException
	 */
	public SimpleModel loadDefault(Class<?> object, String id)
			throws DaoException {
		return hibernateDao.get(object, id);
	}

	/**
	 * 根据条件查询，由mybatis实现
	 * 
	 * @param nameSpace
	 * @param m
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findListByMapDefault(String nameSpace, Map<String, Object> m)
			throws DataAccessException {
		return batisDao.findListByMap(nameSpace, m);
	}

	/**
	 * 获取记录条数，由mybatis实现
	 * 
	 * @param nameSpace
	 * @param m
	 * @return
	 */
	public int selectCountDefault(String nameSpace, Map<String, Object> m)
			throws DataAccessException {
		return batisDao.selectCount(nameSpace, m);
	}

	/**
	 * 查询单个对象，由mybatis实现
	 * 
	 * @param nameSpace
	 * @param id
	 * @return
	 */
	public SimpleModel getDefault(String nameSpace, String id)
			throws DataAccessException {
		return batisDao.get(nameSpace, id);
	}

	/**
	 * 查询对象列表，由mybatis实现
	 * 
	 * @param nameSpace
	 * @param m
	 * @return
	 */
	public List getListDefault(String nameSpace) throws DataAccessException {
		return batisDao.getList(nameSpace);
	}

	public SimpleModel editObj(SimpleModel object) throws DaoException {
		// TODO Auto-generated method stub
		return hibernateDao.editObj(object);
	}

}
